package com.newlink.building.common_base.receiver

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.newlink.building.common_base.constant.Base_Constant
import com.newlink.building.common_base.event.Base_NetworkChangeEvent
import com.newlink.building.common_base.utils.NLog
import com.newlink.building.common_base.utils.NetWorkUtil
import com.newlink.building.common_base.utils.Base_Preference
import org.greenrobot.eventbus.EventBus


/**
 * Created by xiaodong on 2022/7/25.
 */
class Base_NetworkChangeReceiver : BroadcastReceiver() {

    /**
     * 缓存上一次的网络状态
     */
    private var hasNetwork: Boolean by Base_Preference(Base_Constant.HAS_NETWORK_KEY, true)
    private var isFirstReveived: Boolean = true

    override fun onReceive(context: Context, intent: Intent) {
        val isConnected = NetWorkUtil.isNetworkConnected(context, intent)
        if (hasNetwork != isConnected) {
            hasNetwork = isConnected
            NLog.e("jake .##### NetworkChangeReceiver: onReceive isConnected:$isConnected ")
            EventBus.getDefault().post(Base_NetworkChangeEvent(isConnected))
        }
    }
}
